<!--
 * @Description: 
 * @Version: 2.0
 * @Autor: wiz
 * @Date: 2023-10-17 09:59:26
 * @LastEditors: wiz
 * @LastEditTime: 2024-04-22 11:10:25
-->

<template>
  <div class="init-container">
    <o-base-panel
      ref="refBasePanel"
      :config="config"
      :useTable="false"
      @loadFormData="loadFormData"
    />
  </div>
</template>

<script lang="ts">
import {
  Tsrv,
  T_srv,
  ifs,
  ts,
  vue,
  I_cmd,
  T_cmd,
  N_oSetupPanel,
  N_oBaseGrid
} from './config';

@T_srv.comp
export default class cBaseForm extends vue {
  /* 父组件compBoxInfo */
  @T_srv.prop({
    required: false,
    type: Object,
    default: null
  })
  compBoxInfo: ifs.I_compBoxInfo;

  @T_srv.prop({
    required: true
  })
  config!: any;

  /* 组件名 */
  private name: string = 'cBaseForm';

  private G_compName: string = this.name;

  /* 组件箱信息对象 */
  private GT_compBoxInfo: ifs.I_compBoxInfo;

  /* 组件箱对象 */
  private GT_compBox: ifs.I_compBox;

  /* 组件箱识别ID */
  private G_compID: string;

  private listenType: string = 'default';

  /* 指挥部 */
  private GT_command: I_cmd = new T_cmd();

  private messageConfig: N_oSetupPanel.N_comps.I_messageConf = null;

  public funName: string = '';

  // 定制转发事件
  private forwordEvent = {
    search: [],
    refresh: []
  };

  created() {
    // 如果父组件没有传参，那就产生一个
    this.GT_compBoxInfo = Tsrv.getCompInfo(this.compBoxInfo);
    this.G_compID = this.GT_compBoxInfo.compID;
    this.GT_compBox = this.GT_compBoxInfo.TcompBox;
    this.GT_command.init(this, this.GT_compBoxInfo);
    this.init();
  }

  private init() {
    this.funName = this.config?.name || '';
    this.messageConfig =
      Tsrv.utils.clone(this.config.messageConfig, true) || null;
    // 克隆事件本组件接收监听代理
    const search: Array<string> = this.messageConfig.custDefListener.search;
    const refresh: Array<string> = this.messageConfig?.custDefListener?.refresh;
    if (!Tsrv.utils.noValue(search) && Array.isArray(search)) {
      search.forEach(e => {
        this.forwordEvent.search.push(Tsrv.utils.custUUID(16, 16));
      });
    }
    if (!Tsrv.utils.noValue(search) && Array.isArray(refresh)) {
      search.forEach(e => {
        this.forwordEvent.refresh.push(Tsrv.utils.custUUID(16, 16));
      });
    }

    // coordination事件不代理
    this.config.messageConfig.custDefListener.search = this.forwordEvent.search;
    this.config.messageConfig.custDefListener.refresh = this.forwordEvent.refresh;
  }

  mounted() {
    /* 向父组件发送实例 */
    this.GT_compBox.addCompMergeName(this.G_compID, this.G_compName, this);
    this.$emit('compBoxInfo', this.GT_compBoxInfo);
    this.listenEvent();
  }

  loadFormData(res) {
    this.messageEmit('search', [res]);
  }

  listenEvent() {
    if (!Tsrv.utils.noValue(this.messageConfig.custDefListener?.search)) {
      this.messageConfig.custDefListener.search.forEach((e, idx) => {
        this.$bus.$on(e, (msg: any) => {
          this.$bus.$emit(this.forwordEvent.search[idx], msg);
        });
      });
    }
    if (!Tsrv.utils.noValue(this.messageConfig.custDefListener?.refresh)) {
      this.messageConfig.custDefListener.refresh.forEach((e, idx) => {
        this.$bus.$on(e, (msg: any) => {
          this.$bus.$emit(this.forwordEvent.refresh[idx], msg);
        });
      });
    }
    // coordination事件不代理
    // if (!Tsrv.utils.noValue(this.messageConfig.custDefListener?.coordination)) {
    //   this.$bus.$on(
    //     this.messageConfig.custDefListener.coordination,
    //     (msg: any) => {
    //       this.$bus.$emit(this.forwordEvent.coordination, msg);
    //     }
    //   );
    // }
  }

  // 通过事件toID获取要search的功能名
  private getFunNameByToID(toID: string): string {
    for (const o of this.config.messageConfig.emitActions.search) {
      if (o.toID !== toID) {
        continue;
      } else {
        return o.sourceToID;
      }
    }
    return '';
  }

  private messageEmit(type: any, row: any = null) {
    if (this.messageConfig !== null) {
      // 获取所有收信人
      const receivers = this.messageConfig.emitActions[type] || [];
      receivers.forEach((item: any) => {
        item.msg.listenType = this.listenType;
        // 发出的是search信号/refresh信号
        if (type === 'search') {
          // 装填搜索key对应的值
          item.msg.content.val = Tsrv.utils.map(
            row,
            el => el[item.msg.content.ikey]
          );
          if (item.msg.content.val.indexOf(undefined) < 0) {
            const toID = item.toID.toString();
            // option
            if (item.msg.content.val.length === 1) {
              const refBasePanel: any = this.$refs.refBasePanel;
              const option = {
                autoCompleteInfo: {
                  key: item.msg.content.val[0],
                  sourceToID: toID,
                  fromTable: refBasePanel.funName.formName,
                  toTable: this.getFunNameByToID(toID),
                  table: toID,
                  data: row[0]
                }
              };
              item.msg['option'] = option;
            }

            // 发出
            if (this.listenType === 'default') {
              Tsrv.reliablySendingBusEvent(toID, this.$bus, 10, 100, item.msg);
            } else {
              Tsrv.reliablySendingBusEvent(toID, this.$mbus, 10, 100, item.msg);
            }
          }
        } else if (type === 'refresh') {
          const toID = item.toID.toString();
          // refresh信号直接发出
          if (this.listenType === 'default') {
            Tsrv.reliablySendingBusEvent(toID, this.$bus, 10, 100, item.msg);
          } else {
            Tsrv.reliablySendingBusEvent(toID, this.$mbus, 10, 100, item.msg);
          }
        }
      });
    }
  }

  destroyed() {
    /* 注册组件销毁 */
    this.GT_compBox.removeCompMergeName(this.G_compID, this.G_compName);

    /* 如果父组件传入的compBoxInfo则删除本文件产生的引用,反之是本组件产生的ID统一删除 */
    if (Tsrv.utils.isEmpty(this.compBoxInfo)) {
      this.GT_compBox.removeCompsWithCompID(this.G_compID);
    } else {
      this.GT_compBox.removeCompsMergeName(this.G_compID, [
        `'${this.G_compName}'`
      ]);
    }
    if (!Tsrv.utils.noValue(this.messageConfig.custDefListener?.search)) {
      this.messageConfig.custDefListener.search.forEach(e => {
        this.$bus.$off(e);
      });
    }
    if (!Tsrv.utils.noValue(this.messageConfig.custDefListener?.refresh)) {
      this.messageConfig.custDefListener.refresh.forEach(e => {
        this.$bus.$off(e);
      });
    }
  }
}
</script>
<style lang="scss" scoped></style>
